//
//  AXFConponViewController.m
//  AXFEightGroup
//
//  Created by hgj on 2016/12/24.
//  Copyright © 2016年 ya. All rights reserved.
//

#import "AXFConponViewController.h"
#import "AXFDiscountCardCell.h"
#import "MBProgressHUD+Add.h"
#import "AXFUseRegularController.h"

@interface AXFConponViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, weak) UIView *topView;
@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, strong) NSArray<AXFDataModel *> *dataArray;
@property (nonatomic, weak) UITextField *textFiled;

@end

@implementation AXFConponViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"优惠券";
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"使用规则" style:UIBarButtonItemStylePlain target:self action:@selector(useRegulation)];
    
    [self loadData];
    [self setupTopView];
    [self setupTableView];
}

#pragma mark - 使用规则 -
- (void)useRegulation {

        AXFUseRegularController *regularVC = [AXFUseRegularController new];
    UINavigationController *vc = [[UINavigationController alloc] initWithRootViewController:regularVC];
    regularVC.navigationItem.title = @"使用规则";
    regularVC.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(dismiss:)];

    [self showDetailViewController:vc sender:nil];
}

- (void)dismiss:(UIBarButtonItem *)item {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 获取数据 - 
- (void)loadData {
    [_ModelMgr myCoupon:^(NSArray<AXFDataModel *> * _Nullable models) {
        self.dataArray = models;
        [self.tableView reloadData];
    }];
}

#pragma mark - 顶部视图 -
- (void)setupTopView {
    
    UIView *topView = [UIView new];
    
    [self.view addSubview:topView];
    
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(70);
        make.height.equalTo(@40);
    }];
    
    _topView = topView;
    
    UIButton *button = [UIButton buttonWithType:0];
    
    [topView addSubview:button];
    
    [button setBackgroundImage:[UIImage imageNamed:@"v2_coupon_verify_normal"] forState:UIControlStateNormal];
    
    [button setTitle:@"绑定" forState:UIControlStateNormal];
    
    [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    
    [button addTarget:self action:@selector(binding:) forControlEvents:UIControlEventTouchUpInside];
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(topView);
        make.trailing.equalTo(topView).offset(-18);
        make.width.equalTo(@60);
        make.height.equalTo(@30);
    }];
    
    UITextField *textField = [[UITextField alloc] init];
    
    textField.borderStyle = UITextBorderStyleRoundedRect;
    
    textField.font = [UIFont systemFontOfSize:15];
    
    [textField addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingDidEnd];
    
    [topView addSubview:textField];
    
    textField.placeholder = @"请输入优惠券号码";
    
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(topView);
        make.leading.equalTo(topView).offset(18);
        make.right.equalTo(button.mas_left).offset(-10);
    }];
}

- (void)textChange:(UITextField *)textFiled {
    _textFiled = textFiled;
}

// 点击绑定
- (void)binding:(UIButton *)btn {
    [self.view endEditing:YES];
    if (_textFiled.text == nil) {
        [MBProgressHUD showError:@"请输入优惠码" toView:self.view];
    } else {
        [MBProgressHUD showError:@"请输入正确的优惠码" toView:self.view];
    }
}

#pragma mark - UITableView -
- (void)setupTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    
    [self.view addSubview:tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.topView.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
        
    }];
    
    _tableView = tableView;
    
    tableView.dataSource = self;
    
    [tableView registerNib:[UINib nibWithNibName:@"AXFInvalidDiscountCard" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"invalidDiscountCellID"];
    
    [tableView registerNib:[UINib nibWithNibName:@"AXFValidDiscountCard" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"validDiscountCellID"];
    
    tableView.estimatedRowHeight = 150;
    tableView.rowHeight = UITableViewAutomaticDimension;
}

#pragma mark - UITableViewDeleDelegate -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_dataArray) {
        return self.dataArray.count;
    }
    return 0;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *cellID = [NSString string];
    
    AXFDataModel *model = self.dataArray[indexPath.row];
    
// //// NXTDiscountModel *model = self.disModels[indexPath.row];
    
    if([model.status isEqualToString:@"0"])
    {
        cellID = @"validDiscountCellID";
    } else {
        
        cellID = @"invalidDiscountCellID";
    }
    
    AXFDiscountCardCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.nameLabel.text = self.dataArray[indexPath.row].name;
    
    cell.startLabel.text = self.dataArray[indexPath.row].start_time;
    
    cell.endLabel.text = self.dataArray[indexPath.row].end_time;
    
    cell.descLabel.text = self.dataArray[indexPath.row].desc;

    cell.valueLabel.text = self.dataArray[indexPath.row].value;
    
    return cell;
}


@end
